<?php
/***
* @version $Id: usercp_sendactivation.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2005 - 2006 {james} < james3785@gmail.com > (Laurence Herbert) http://www.vapour-online.com
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

if ( !defined('IN_R3BORN') )
{
   die('Hacking attempt');
}

if ( $config['require_activation'] == USER_ACTIVATION_ADMIN)
{
   message_die(GENERAL_MESSAGE, $lang['login_Admin_activation_only']);
}

if ( isset($_POST['submit']) )
{
   $username = ( !empty($_POST['username']) ) ? r3born_clean_username($_POST['username']) : '';
   $email = ( !empty($_POST['email']) ) ? trim(htmlspecialchars($_POST['email'])) : '';

   $sql = "SELECT user_id, username, user_email, user_active, user_lang
	  FROM " . USERS_TABLE . "
	  WHERE user_email = '" . str_replace("\'", "''", $email) . "'
	  AND username = '" . str_replace("\'", "''", $username) . "'";

   if ( $result = $db->sql_query($sql) )
   {
      $row = $db->sql_fetchrow($result);

      if (!empty($row['user_id']))
      {
         if ($row['user_active'])
         {
            message_die(GENERAL_MESSAGE, $lang['login_No_send_account_active'] . '<br /><br />' . sprintf($lang['Click_return_index'],  '<a href="' . append_sid("index.$phpEx") . '">', '</a>'));
         }

         $username = $row['username'];
         $user_id = $row['user_id'];

         $user_actkey = gen_rand_string(true);
         $key_len = 54 - (strlen($server_url));
         $key_len = ( $key_len > 6 ) ? $key_len : 6;
         $user_actkey = substr($user_actkey, 0, $key_len);

         $sql = "UPDATE " . USERS_TABLE . "
               SET user_actkey = '" . $user_actkey . "'
               WHERE user_id = " . $user_id;
         if ( !$db->sql_query($sql) )
         {
            message_die(GENERAL_ERROR, 'Could not update new activation information', '', __LINE__, __FILE__, $sql);
         }

         include($root_path . 'includes/class_emailer.'.$phpEx);
         $emailer = new emailer($config['smtp_delivery']);

         $emailer->from($config['board_email']);
         $emailer->replyto($config['board_email']);

         $emailer->use_template('user_activate_resend', $row['user_lang']);
         $emailer->email_address($row['user_email']);

         $emailer->assign_vars(array(
            'SITENAME' => $config['sitename'],
            'USERNAME' => $username,
            'EMAIL_SIG' => (!empty($config['board_email_sig'])) ? str_replace('<br />', "\n", "-- \n" . $config['board_email_sig']) : '',

            'U_ACTIVATE' => $server_url . '?mode=activate&' . POST_USERS_URL . '=' . $user_id . '&act_key=' . $user_actkey)
         );
         $emailer->send();
         $emailer->reset();

         $template->assign_vars(array(
            'META' => '<meta http-equiv="refresh" content="3;url=' . append_sid("index.$phpEx") . '">')
         );

         $message = $lang['login_NewActivationSent'] . '<br /><br />' . sprintf($lang['Click_return_index'],  '<a href="' . append_sid("index.$phpEx") . '">', '</a>');

         message_die(GENERAL_MESSAGE, $message);
      }
      else
      {
         $template->assign_vars(array(
            'META' => '<meta http-equiv="refresh" content="3;url=' . append_sid("register.$phpEx?mode=resendactivation") . '">')
         );

         $message = $lang['login_No_Such_User'] . '<br /><br />' . sprintf($lang['Click_return_login'],  '<a href="' . append_sid("register.$phpEx?mode=resendactivation") . '">', '</a>');

         message_die(GENERAL_MESSAGE, $message);
      }
   }
   else
   {
      message_die(GENERAL_ERROR, 'Could not obtain user information for sendpassword', '', __LINE__, __FILE__, $sql);
   }
}
else
{
   $username = '';
   $email = '';
}

//
// Output basic page
//
page_header();

$template->set_filenames(array(
   'body' => 'profile_resendactivation.tpl')
);

//make_jumpbox('viewforum.'.$phpEx);

$template->assign_vars(array(
   'USERNAME' => $username,
   'EMAIL' => $email,

   'L_SEND_ACTIVATION' => $lang['login_Resend_Activation'],
   'L_ITEMS_REQUIRED' => $lang['Items_required'],
   'L_EMAIL_ADDRESS' => $lang['Email_address'],
   'L_SUBMIT' => $lang['Submit'],
   'L_RESET' => $lang['Reset'],

   'S_HIDDEN_FIELDS' => '',
   'S_PROFILE_ACTION' => append_sid("register.$phpEx?mode=resendactivation"))
);

$template->pparse('body');

page_footer();

?>